package com.yunxin.download.server.infra.support.filed.transfer.impl;

import com.yunxin.download.server.service.extenal.FileExportConfigDTO;
import com.yunxin.download.server.infra.support.filed.transfer.FieldTransfer;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @author zero°
 * @since 2023-06-15
 */
@Slf4j
public class LocalDateTimeTransfer implements FieldTransfer<String, LocalDateTime> {

    private static final String INIT_DATE = "1970-01-01";

    @Override
    public String transfer(LocalDateTime value, FileExportConfigDTO.FieldFormat format) {
        final Map<?, ?> param = format.getParam();
        if (param != null) {
            final String pattern = (String)param.get("pattern");
            if (pattern != null) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
                String date = value.format(formatter);
                return date.contains(INIT_DATE) ? "" : date;
            } else {
                log.warn("没有日期格式符");
            }
        }
        return "";
    }
}
